home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / APP / S-Z / The Regulator.cpt / The Regulator source / main.c < prev    next >
C/C++ Source or Header  |  1992-06-19  |  9KB  |  239 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #   Apple Developer Technical Support
  4. #
  5. # SmallDaemon
  6. # A small faceless background-only application
  7. #
  8. #   main.c  -   C Source
  9. #
  10. #   Copyright © 1991 Apple Computer, Inc.
  11. #   All rights reserved.
  12. #
  13. #   Versions:   
  14. #               1.0               09/91       C.K. Haun <TR>
  15. #   
  16. #   Components:
  17. #                           main.c
  18. #                           AppleEvents.c
  19. #
  20. # This is a _very_ simple sample showing a do-nothing 
  21. # System 7.0 faceless background application.  
  22. # The main things to note are....
  23. # 1)  Look at the SIZE resource for the flag settings you'll
  24. #  need to let the Finder™ know that you only want to work
  25. #  in the background.
  26. # 2) Notice that you really do have your own heap and your own
  27. #  event loop.  You can do anything a foreground application
  28. #  can do, send AppleEvents, PPC stuff, anything else you'd
  29. #  like EXCEPT a graphic interface.
  30. # 3)  NOTE that no managers are started up.  You cannot start up
  31. #  Window, Menu, Dialogs, or anything else that 
  32. #  deals with the graphic front end.  Just leave them out.
  33. #  You _can_ start up QuickDraw, if you want to use some QuickDraw
  34. #  functions (like offscreen ports), but you CANNOT actually 
  35. #  do any screen drawing.
  36. #
  37. # Of course, a backgrounder can be launched from the Finder™ 
  38. # with a double-click.  However, if you'd like the backgrounder
  39. # to perform some useful service for your main application, driver,
  40. # DA, or whatever, you will want it running all the time.
  41. #  The BEST way to insure this is to install the backgrounder in the
  42. # StartUp Items folder in the system folder.  This will insure that
  43. # it is always launched, and it will also reduce memory fragmentation
  44. # since it will be installed at startup time.  You can search for it
  45. # when you need it with IPCListPorts (see the PPC toolbox documentation).
  46. # Optionally, you can use the LaunchApplication trap to launch it 
  47. # when you need it, and kill it when you're done.  But this could 
  48. # cause some multiFinder heap fragmentation.
  49. #
  50. # And remember, it's a backgrounder, you can't see it.  To kill it
  51. # use TaskIt or ProcDoggie.
  52. #
  53. # ---------------------------------------------------------------
  54. #   Use this sample as a starting point, and adapt its' routines to 
  55. #   meet the specific needs of your project.  
  56. #
  57. ------------------------------------------------------------------------------*/
  58.  
  59. /* This doesn't do much but wait for a quit event.  But, you can expand it */
  60. /* to suit whatever your needs are for a backgrounder.  */
  61. /* Be sure to look at the SIZE resource for this app to see how the */
  62. /* flags should be set for a background-only task */
  63. #include <memory.h>
  64. #include <appleevents.h>
  65. #include <events.h>
  66. #include <types.h>
  67. #include <gestaltequ.h>
  68. #include "pb.h"
  69.  
  70. /* variables */
  71. Boolean gQuit = false;
  72. EventRecord gERecord;
  73. Boolean gHasAppleEvents;
  74. unsigned long gMySleep = 120;  /* long time, long time.  Change this if  */
  75. /* you'd like to do null processing. Just keep in mind that the */
  76. /* user does NOT know that you exist, and if you are eating */
  77. /* up a bunch of time the user will not know why his or her */
  78. /* machine is slowing down. */
  79.  
  80. /* structs */
  81. /* a little struct to install handlers from.  Makes it easier to plug in */
  82. /* new handlers */
  83. struct AEinstalls {
  84.     AEEventClass theClass;
  85.     AEEventID theEvent;
  86.     EventHandlerProcPtr theProc;
  87. };
  88. typedef struct AEinstalls AEinstalls;
  89.  
  90. /* prototypes for this file */
  91. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  92. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  93. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  94. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  95. void DoHighLevel(EventRecord *AERecord);
  96. void InitAEStuff(void);
  97.  
  98. main()
  99. {
  100.     /* We are NOT initializing any managers.  We're in the background, with no */
  101.     /* face, we can't use windows or dialogs or menus.  If you need to talk to the */
  102.     /* user you can post a notification, or launch an application to communicate */
  103.     /* Passing an AppleEvent in the launchapplication trap could do the */
  104.     /* communication for you. */
  105.     
  106.     InitGraf((Ptr) &thePort);
  107.     /* Routine to install Apple event handlers */
  108.     InitAEStuff();
  109.     
  110.     InitPortableStuff();
  111.  
  112.     /* no nothing but high level events */
  113.     while (gQuit == false) {
  114.         WaitNextEvent(highLevelEventMask, &gERecord, gMySleep, 0);
  115.         CheckForMains();
  116.         switch (gERecord.what) {                    
  117.             case kHighLevelEvent:
  118.             DoHighLevel(&gERecord);
  119.             break;
  120.         }
  121.     }
  122. }
  123.  
  124. /* Here are the AppleEvent handlers for this background-only task. */
  125. /* Since this is a real live application, it can accept and send any */
  126. /* Apple Events you want to send or receive.  */
  127. /* In this case, the only ones that make any sense in this app are */
  128. /* 'oapp' and 'quit' */
  129.  
  130.  
  131. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  132. /* installs our event handlers. */
  133. /* if the AEM isn't around, we bail. */
  134. void InitAEStuff(void)
  135. {
  136.     static AEinstalls HandlersToInstall[] =  {
  137.          {
  138.             kCoreEventClass, kAEOpenApplication, AEOpenHandler
  139.         },  {
  140.             kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
  141.         },  {
  142.             kCoreEventClass, kAEQuitApplication, AEQuitHandler
  143.         },  {
  144.             kCoreEventClass, kAEPrintDocuments, AEPrintHandler
  145.         }, 
  146.         /* The above are the four required AppleEvents. */
  147.     };
  148.     
  149.     OSErr aevtErr = noErr;
  150.     long aLong = 0;
  151.     Boolean gHasAppleEvents = false;
  152.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  153.     *   then we exit */
  154.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  155.     /* The following series of calls installs all our AppleEvent Handlers.
  156.     *   These handlers are added to the application event handler list that 
  157.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  158.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  159.     *   list of handlers and dispatch to it if there is one.
  160.     */
  161.     if (gHasAppleEvents) {
  162.         register qq;
  163.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  164.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  165.                                             HandlersToInstall[qq].theProc, 0, false);
  166.             if (aevtErr) {
  167.                 /* Of course, you can't tell the user why you died directly, since you have no face.  */
  168.                 /* But, you can post a notification from here to let them know. */
  169.                 ExitToShell();                              /* just fail, baby */
  170.             }
  171.         }
  172.     } else {
  173.         ExitToShell();
  174.     }
  175. }
  176.  
  177. /* end InitAEStuff */
  178.  
  179. void DoHighLevel(EventRecord *AERecord)
  180. {
  181.     /* I'm not doing any error handling here because there's not a lot */
  182.     /* I can do, just pass the errors back. */
  183.     AEProcessAppleEvent(AERecord);
  184.     
  185. }
  186.  
  187. /* end DoHighLevel */
  188.  
  189.  
  190. /* This is the standard Open Application event.   */
  191. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  192. {
  193. #pragma unused (messagein,reply,refIn)
  194.     /* we of course don't do anything here, since we're background only */
  195.     /* But this was a normal launch, so return noErr */
  196.     return(noErr);
  197. }
  198.  
  199. /* end AEOpenHandler */
  200.  
  201. /* Open Doc, opens our documents. */
  202. /* In this case, of course, you are in the background so you should return an error */
  203. /* here since you're not opening a document. */
  204. /* Of course, you _might_ want to open a doc, but you will probably */
  205. /* confuse the user if you do, since they will see no action as the */
  206. /*  result of their clicking on a document icon. */
  207. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  208. {
  209. #pragma unused (reply, refIn,messagein)
  210.     /* we of course don't do anything here, so tell the sender that we  */
  211.     /* didn't handle the event */
  212.     return(errAEEventNotHandled);
  213. }
  214.  
  215. /* same logic for printing */
  216. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  217. {
  218. #pragma unused (reply,refIn,messagein)
  219.     /* we of course don't do anything here */
  220.     return(errAEEventNotHandled);
  221. }
  222.  
  223. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  224. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  225. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  226. {
  227. #pragma unused (messagein,refIn,reply)
  228.     extern Boolean Quit;
  229.     /*  This does _NOT_ quit, you */
  230.     /* should NEVER quit from an AppleEvent handler.  Calling */
  231.     /* ExitToShell here would blow up the Finder™ */
  232.     SetRest();
  233.     SetSysSleep(gSysSleep);
  234.     SetHDSleep(gHDSleep);
  235.     gQuit = true;
  236.     return(noErr);
  237. }
  238.